﻿using System.Reflection;
using Wedn.Core.Utility;
using Wedn.Interface.DAL;

namespace Wedn.Interface
{
    /// <summary>
    /// 数据库操作对象工厂类
    /// </summary>
    public class DAOFactory
    {
        //public static IPost CreateDAO(string DbType)
        //{
        //    IPost dao = null;
        //    dao = (IPost)Assembly.Load("Wedn.Lib.DAL.MSSQL").CreateInstance(DbType);
        //    return dao;
        //}
        private static readonly string path = "Wedn.Lib.DAL.MSSQL";//Config.DALPath;
        private static string ns = "Wedn.DAL.MSSQL";//Config.DALNameSpace;
        private static readonly string nspace = string.IsNullOrEmpty(ns) ? path : ns;

        private static object lockHelper = new object();

        public static IOption iOption = null;
        public static IPost iPost = null;
        public static IUser iUser = null;
        public static IComment iComment = null;
        public static ILink iLink = null;
        public static IMeta iMeta = null;
        public static ITerm iTerm = null;
        public static IRelation iRelation = null;
        /// <summary>
        /// 创建实例
        /// </summary>
        public static IOption CreateOptionDAOInstance()
        {
            string className = nspace + ".OptionDAO";
            //return (ITag)Assembly.Load(path).CreateInstance(className);
            return CreateInstance<IOption>(iOption, className);
        }
        /// <summary>
        /// 创建实例
        /// </summary>
        public static IPost CreatePostDAOInstance()
        {
            string className = nspace + ".PostDAO";
            return CreateInstance<IPost>(iPost, className);
        }
        /// <summary>
        /// 创建实例
        /// </summary>
        public static IUser CreateUserDAOInstance()
        {
            string className = nspace + ".UserDAO";
            return CreateInstance<IUser>(iUser, className);
        }
        /// <summary>
        /// 创建实例
        /// </summary>
        public static IComment CreateCommentDAOInstance()
        {
            string className = nspace + ".CommentDAO";
            return CreateInstance<IComment>(iComment, className);
        }
        /// <summary>
        /// 创建实例
        /// </summary>
        public static ILink CreateLinkDAOInstance()
        {
            string className = nspace + ".LinkDAO";
            return CreateInstance<ILink>(iLink, className);
        }
        /// <summary>
        /// 创建实例
        /// </summary>
        public static IMeta CreateMetaDAOInstance()
        {
            string className = nspace + ".MetaDAO";
            return CreateInstance<IMeta>(iMeta, className);
        }
        /// <summary>
        /// 创建实例
        /// </summary>
        public static IRelation CreateRelationDAOInstance()
        {
            string className = nspace + ".RelationDAO";
            return CreateInstance<IRelation>(iRelation, className);
        }
        /// <summary>
        /// 创建实例
        /// </summary>
        public static ITerm CreateTermDAOInstance()
        {
            string className = nspace + ".TermDAO";
            return CreateInstance<ITerm>(iTerm, className);
        }

        /// <summary>
        /// 实例化, 如果存在则返回对象, 不存在则返回null;
        /// </summary>
        /// <returns>对象</returns>
        private static T CreateInstance<T>(T instance, string className)
        {
            if (instance == null)
            {
                lock (lockHelper)
                {
                    if (instance == null)
                    {
                        instance = (T)Assembly.Load(path).CreateInstance(className);
                    }
                }
            }
            return instance;
        }
    }
}
